﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;


public class SessionService
{
    private static SessionService instance = null;
    private Dictionary<int, User> sessions;
    private Random random;

	private SessionService()
	{
        sessions = new Dictionary<int, User>();
        random = new Random();
	}

    public static SessionService getInstance()
    {
        if (instance == null)
        {
            instance = new SessionService();
        }
        return instance;
    }

    public int createSession(User user)
    {
        int hashedKey = random.Next();
        if (sessions.ContainsKey(hashedKey))
        {
            return createSession(user);
        }
        else
        {
            sessions.Add(hashedKey, user);
        }

        return hashedKey;
    }

    public User getUserFromSession(int sessionKey)
    {
        if (sessions.ContainsKey(sessionKey))
        {
            return sessions[sessionKey];
        }
        else
        {
            return null;
        }
    }

}
